package com.note.simpleadmin.module.shop.service;

import com.note.simpleadmin.entity.TbBanner;
import com.note.simpleadmin.entity.TbBrand;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.dao.BrandRepository;
import com.note.simpleadmin.utils.IdGenerator;
import com.note.simpleadmin.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/***
 *@ClaseName:BrandService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/31 0031 20:59
 */
@Service
public class BrandService {
    @Autowired
    private BrandRepository brandRepository;

    public TbBrand getOne(String id)
    {
        return brandRepository.getOne(id);
    }
    public void delete(String id) throws DataNotExistException
    {
        TbBrand brand=brandRepository.getOne(id);
        if(brand==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        brandRepository.delete(brand);
    }
    public void update(TbBrand brand)throws DataNotExistException
    {
        TbBrand brand1=brandRepository.getOne(brand.getId());
        if(brand1==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        brandRepository.save(brand);
    }
    public void save(TbBrand brand)
    {
        brand.setId(IdGenerator.id());
        brandRepository.save(brand);
    }
    public List<TbBrand> findAll()
    {
        return brandRepository.findAll();
    }
    public PageBean findByPage(String name,Integer pageNumber)
    {
        if (pageNumber == null) {
            pageNumber = 0;
        }
        else
        {
            pageNumber=pageNumber-1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        PageRequest pageRequest = new PageRequest(pageNumber, 20, sort);
        Specification<TbBrand> specification=new Specification<TbBrand>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<TbBrand> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(name)) {
                    predicates.add(criteriaBuilder.equal(root.get("name"), name));
                }
                return  criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<TbBrand> page = brandRepository.findAll(specification,pageRequest);
        List<TbBrand> environments=page.getContent();
        PageBean<TbBrand> pageBean=new PageBean(page.getTotalPages(),page.getTotalElements(),page.isFirst(),page.isEmpty(),page.getNumber(), environments);;
        return pageBean;
    }

}
